package com.example.courseselectionsystemconsume.service.impl;

import com.example.courseselectionsystemconsume.dao.CourseDao;
import com.example.courseselectionsystemconsume.dao.CourseRecordDao;
import com.example.courseselectionsystemconsume.dao.UserDao;
import com.example.courseselectionsystemconsume.pojo.CourseRecord;
import com.example.courseselectionsystemconsume.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @description: TODO
 * @author: Tokyo_God
 * @date: 2021/1/10 11:12
 * Role of:
 * @version: v1.0
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    CourseDao courseDao;
    @Resource
    CourseRecordDao courseRecordDao;
    @Resource
    UserDao userDao;

    //选课   （可以考虑在选课的时候加学分）以加
    @Transactional   //开启事务
    @Override
    public int takeTheCourse(CourseRecord courseRecord) {
        int num=0;
        /*try {*/
            //课程数量加1  在修改课程的数量
            int num1=courseRecordDao.addCourseRecord(courseRecord);//添加记录
            int num2=courseDao.upCourseAmount(courseRecord.getCourseId());//修改数量
            int num3=userDao.upUserMark(courseRecord.getUserId(),courseRecord.getWorth());//修改分数
        System.out.println((num1>0?"添加记录成功":"添加记录失败")+";"+(num2>0?"修改数量成功":"修改数量失败")+";"+(num3>0?"修改分数成功":"修改分数失败"));
        if(num1>0&&num2>0&&num3>0){
            num=1;
        }
            /*int a=1/0;*/
        /*}catch (Exception e){
            System.out.println("进入异常");
            num=0;
        }*/
        return num;
    }
}
